﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CarbonExchange.Bll
{
    /// <summary>
    /// Configuration of Audit Questions powering customisable text
    /// </summary>
    public class QuestionTextConfiguration
    {
        /// <summary>
        /// Energy:100-199
        /// Transport:200-299
        /// Waste:300-399
        /// Water:400-499
        /// Shopping:500-599
        /// </summary>
        public enum QuestionTypes : int
        { 
            Unknown =0,
            //Energy
            Electricity = 100,
            Heating = 101,
            Energy = 102,
            //Transport
            Flights = 200,
            Cars = 201,
            Motorcycles = 202,
            BusTravel = 203,
            LocalTrainTravel = 204,
            IntercityTrainTravel = 205,
            UndergroundTrainTravel = 206,
            TaxiTravel = 207,
            //Waste
            HouseholdWaste = 300,
            GardenWaste = 301,
            RecycleGlassWaste = 302,
            RecyclePaperCardWaste = 303,
            RecycleMetal=304,
            RecyclePlasticWaste=305,
            //Water
            Water=400,
            //Shopping
            PerishablesShopping = 500,
            ClothingShopping = 501,
            HouseholdShopping=502,
            LeisureShopping=503,

        }   

        /// <summary>
        /// Inialise Configuration from Question Type Question Database Id and Site Id
        /// </summary>
        /// <param name="question">The Question Type value</param>
        /// <param name="questionId">the question Id</param>
        /// <param name="siteId"></param>
        public QuestionTextConfiguration(QuestionTypes questionType, int questionId, int siteId)
        {
            ConfigureQuestion(questionType, questionId, siteId);
        }

        /// <summary>
        /// Inialise Configuration supplying the Question Record wich contains QuestionType and Question Id
        /// </summary>
        /// <param name="question"><see cref="Question"/></param>
        /// <param name="siteId">Site Id</param>
        public QuestionTextConfiguration(SiteQuestion question)
        {
            ConfigureQuestion(question.QuestionType, question.QuestionId, question.SiteId);
        }

        /// <summary>
        /// Inialise Configuration supplying the Question Record wich contains QuestionType and Question Id
        /// </summary>
        /// <param name="question"><see cref="Question"/></param>
        /// <param name="siteId">Site Id</param>
        public QuestionTextConfiguration(Question question, int siteId)
        {
            ConfigureQuestion(question.QuestionType, question.Id, siteId);
        }

        /// <summary>
        /// Private method initalises the properties from switch statment.
        /// </summary>
        /// <param name="question"></param>
        /// <param name="questionId"></param>
        /// <param name="siteId"></param>
        private void ConfigureQuestion(QuestionTypes questionType, int questionId, int siteId)
        {
            this.QuestionType = questionType;
            this.QuestionId = questionId;
            TextValues = new List<AuditQuestionCustomText>();
            switch (questionType)
            {
                //Energy
                /*case QuestionTypes.Electricity:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Energy/Electricity.ascx";
                    TextValues.Add(CreateValue("Green Tariff", "Do you purchase your electricity on a green tariff?"));
                    TextValues.Add(CreateValue("Green Tariff Help", "If you purchase on a green traiff its <b>\"Free\"</b>"));
                    TextValues.Add(CreateValue("Elecricity Bills", "Do you have a copy of your recent electricity bill available?"));   
                    TextValues.Add(CreateValue("Elecricity Bills Help", "By providing acurate figures from your bills the footprint calcuation is very acurate"));
                    TextValues.Add(CreateValue("Elecricity Bills What Period", "What time period does the bill cover?"));
                    TextValues.Add(CreateValue("Elecricity Bills Value", "How many kWh were used during that period?"));
                    TextValues.Add(CreateValue("Standard Light Bulbs", "How many standard (non energy efficient) light bulbs do you have in your home?"));
                    TextValues.Add(CreateValue("Energy Efficient Light Bulbs", "How many energy efficient light bulbs do you have in your home?"));
                    TextValues.Add(CreateValue("Computer Equipment", "Please enter all computer equipment you have at home:"));
                    TextValues.Add(CreateValue("Appliances", "Please enter all major appliances you have at home:"));
                    TextValues.Add(CreateValue("Cooking Appliances", "Please tick the type of hob and oven you have in your home:"));
                    TextValues.Add(CreateValue("TV Equipment", "Please enter all television equipment you have in your household:"));
                    TextValues.Add(CreateValue("Other Equipment", "Please enter all other electrical appliances you have in your household:"));
                    break;
                case QuestionTypes.Heating:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Energy/Heating.ascx";
                    TextValues.Add(CreateValue("Primary Heating Source", "How do you primarily heat your home?"));
                    TextValues.Add(CreateValue("Heating Bills", "Do you have a copy of your recent heating bill available?"));
                    TextValues.Add(CreateValue("Heating Bills What Period", "What time period does the bill cover?"));
                    TextValues.Add(CreateValue("Heating Bills Value", "How much energy was used during that period?"));
                    TextValues.Add(CreateValue("Home Type", "What type of home do you live in?"));
                    TextValues.Add(CreateValue("No Bedrooms", "How many bedrooms does your home have?"));
                    TextValues.Add(CreateValue("Home Age", "How old is your home?"));
                    TextValues.Add(CreateValue("Heater Type", "Type of Boiler / Heater?"));
                    TextValues.Add(CreateValue("Energy Technologies", "Please tick all of the renewable energy technologies you have installed in your home:"));
                    TextValues.Add(CreateValue("Insulation", "Please tick any of the following insulation you have installed in your home:"));
                    break;*/
                case QuestionTypes.Energy:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Energy/Energy.ascx";
                    TextValues.Add(CreateValue("Green Tariff", "Do you purchase your electricity on a green tariff?"));
                    TextValues.Add(CreateValue("Green Tariff Help", "All major energy suppliers offer a green tariff, for example British Gas Zero Carbon; E.ON Go Green; n-power Juice; Scottish Power Green Energy H2O; Ecotricity."));
                    TextValues.Add(CreateValue("Elecricity Bills", "Do you have a copy of your recent electricity bill available?"));
                    TextValues.Add(CreateValue("Elecricity Bills Help", "Ideally please provide electricity usage data for one year. If not, please provide for as long a period as possible. This data should be available from most electricity bills."));
                    TextValues.Add(CreateValue("Elecricity Bills What Period", "What time period does the bill cover?"));
                    TextValues.Add(CreateValue("Elecricity Bills Value", "How many kWh were used during that period?"));
                    TextValues.Add(CreateValue("Standard Light Bulbs", "How many standard (non energy efficient) light bulbs do you have in your home?"));
                    TextValues.Add(CreateValue("Standard Light Bulbs Help", "Please include all light fittings and lamps and include every room and cupboard in the house, including any garages."));
                    TextValues.Add(CreateValue("Energy Efficient Light Bulbs", "How many energy efficient light bulbs do you have in your home?"));
                    TextValues.Add(CreateValue("Energy Efficient Light Bulbs Help", "Please include all light fittings and lamps and include every room and cupboard in the house, including any garages."));
                    TextValues.Add(CreateValue("Computer Equipment", "Please enter all computer equipment you have at home:"));
                    TextValues.Add(CreateValue("Computer Equipment Help", "Please consider all of the occupants of your home when answering this question."));
                    TextValues.Add(CreateValue("Appliances", "Please enter all major appliances you have at home:"));
                    TextValues.Add(CreateValue("Appliances Help", "Please consider all of the appliances in your home, including any appliances in cupbaords or in garages."));
                    TextValues.Add(CreateValue("Cooking Appliances", "Please tick the type of hob and oven you have in your home:"));
                    TextValues.Add(CreateValue("Cooking Appliances Help", "Hobs are also know as stoves. Under electric ovens do not include electric microwave ovens, which should be covered in question about Appliances above."));
                    TextValues.Add(CreateValue("TV Equipment", "Please enter all television equipment you have in your household:"));
                    TextValues.Add(CreateValue("TV Equipment Help", "Please consider every room in your house."));
                    TextValues.Add(CreateValue("Other Equipment", "Please enter all other electrical appliances you have in your household:"));
                    TextValues.Add(CreateValue("Other Equipment Help", "Please consider every room in your house."));
                    TextValues.Add(CreateValue("Primary Heating Source", "How do you primarily heat your home?"));
                    TextValues.Add(CreateValue("Primary Heating Source Help", "Most homes will be heated by gas or electricity, but a minority might be heated by alterantive sources such as coal, LPG or oil."));
                    TextValues.Add(CreateValue("Heating Bills", "Do you have a copy of your recent heating bill available?"));
                    TextValues.Add(CreateValue("Heating Bills Help", "Ideally please provide data for one year. If not, please provide for as long a period as possible. This data should be available from most heating bills. If your home is heated by electricity and you submitted bill data, this will already have been covered."));
                    TextValues.Add(CreateValue("Heating Bills What Period", "What time period does the bill cover?"));
                    TextValues.Add(CreateValue("Heating Bills Value", "How much energy was used during that period?"));
                    TextValues.Add(CreateValue("Home Type", "What type of home do you live in?"));
                    TextValues.Add(CreateValue("Home Type Help", "This relates to the type and size of building you live in. For example, is it detached, semi detached or an apartment."));
                    TextValues.Add(CreateValue("No Bedrooms", "How many bedrooms does your home have?"));
                    TextValues.Add(CreateValue("Home Age", "How old is your home?"));
                    TextValues.Add(CreateValue("Heater Type", "Type of Boiler / Heater?"));
                    TextValues.Add(CreateValue("Energy Technologies", "Please tick all of the renewable energy technologies you have installed in your home:"));
                    TextValues.Add(CreateValue("Energy Technologies Help", "It is likely that you will not have any renewable energy technologies installed. However, some homes will have technologies such as Biomass heating, Ground source heat pump, Hydro, Photovoltaic solar panels, Solar hot water, Wind turbine."));
                    TextValues.Add(CreateValue("Insulation", "Please tick any of the following insulation you have installed in your home:"));
                    TextValues.Add(CreateValue("Insulation Help", "Please consider all of the insulation features that you are aware of in your home. It might be insulation from heat or from the cold."));
                    break;
                
                
                //Transport
                case QuestionTypes.Flights:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/Flights.ascx";
                    TextValues.Add(CreateValue("How many flights", "How many flights (including each flight leg) did your household take last year?"));
                    TextValues.Add(CreateValue("How many flights help", "Each flight leg should be accounted for separately to ensure the calculation is accurate. For example, if your household flew from London to New York via Amsterdam, you would enter two separate return flights - one from London to Amsterdam and one from Amsterdam to New York."));

                    TextValues.Add(CreateValue("How many people", "How many people from your household were on the flight?"));
                    TextValues.Add(CreateValue("How many times a year", "How many times a year do you take this flight?"));
                    TextValues.Add(CreateValue("Offset", "Was the flight offset?"));
                    break;
                case QuestionTypes.Cars:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/Cars.ascx";
                    TextValues.Add(CreateValue("How many cars", "How many cars are there in your household?"));
                    TextValues.Add(CreateValue("How many cars help", "Please include all vehicles, including vans, camper vans and company cars."));

                    


                    TextValues.Add(CreateValue("Fuel", "What type of fuel does it run on?"));
                    TextValues.Add(CreateValue("Engine", "What size is the engine?"));
                    TextValues.Add(CreateValue("How far per year", "Approximately how many miles/kms do you drive in a year?"));
                    TextValues.Add(CreateValue("Serviced regularly", "Do you have your car(s) serviced regularly?"));
                    TextValues.Add(CreateValue("Serviced regularly help", "Your vehicle manual will recommend how often you need to have it serviced - it may be after a certain length of time, or after a certain number of miles have been driven."));

                    

                    TextValues.Add(CreateValue("Tyres inflated correctly", "Do you usually keep your car tyres inflated to the correct pressure?"));
                    TextValues.Add(CreateValue("Tyres inflated correctly help", "Your vehicle manual will recommend how often you need to have it serviced - it may be after a certain length of time, or after a certain number of miles have been driven."));
                    
                    TextValues.Add(CreateValue("Eco Driving", "Do you generally try to drive smoothly, avoiding excessive speed, acceleration and sudden braking?"));
                    TextValues.Add(CreateValue("Eco Driving help", "A good indication of this is the engine's RPM when you change gear. Smooth driving should mean changing up the gears within 2500RPM (petrol) or 2000RPM (diesel)."));
                    

                    TextValues.Add(CreateValue("Air Conditioning", "Do you usually use air conditioning in your car(s)?"));
                    TextValues.Add(CreateValue("Air Conditioning help", "Do you use air conditioning to cool down regardless of your speed, or would you wind down the windows at lower speeds (below 35MPH)?"));



                    TextValues.Add(CreateValue("Car Share", "Do you frequently share a car with non-household members?"));
                    TextValues.Add(CreateValue("Car Share help", "Do you make an effort to share journeys with other people (family, friends or colleagues) to reduce the number of car journeys that are necesssary?"));

                    
                    
                    TextValues.Add(CreateValue("No of People", "What is the average number of people you have in the car at one time?"));
                    TextValues.Add(CreateValue("No of People help", "Please include an petrol / diesel powered motorcycle, including mopeds or scooters."));


                    break;
                case QuestionTypes.Motorcycles:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/Motorcycles.ascx";
                    TextValues.Add(CreateValue("How many motorcycles", "How many motorcycles are there in your household?"));
                    TextValues.Add(CreateValue("Engine", "What size is the engine?"));
                    TextValues.Add(CreateValue("How far per year", "Approximately how many miles/kms do you drive in a year?"));
                    break;
                case QuestionTypes.BusTravel:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/BusTravel.ascx";
                    TextValues.Add(CreateValue("Bus Travel Total", "On average, how far does your household travel by bus each month?"));
                    break;
                case QuestionTypes.LocalTrainTravel:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/LocalTrainTravel.ascx";
                    TextValues.Add(CreateValue("Local Train Total", "On average, how far does your household travel by local (commuter) train each month?"));
                    break;
                case QuestionTypes.IntercityTrainTravel:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/IntercityTrainTravel.ascx";
                    TextValues.Add(CreateValue("Intercity Train Total", "On average, how far does your household travel by intercity (express) train each month?"));
                    break;
                case QuestionTypes.UndergroundTrainTravel:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/UndergroundTrainTravel.ascx";
                    TextValues.Add(CreateValue("Underground Train Total", "On average, how far does your household travel by underground / subway each month?"));
                    break;
                case QuestionTypes.TaxiTravel:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Transport/TaxiTravel.ascx";
                    TextValues.Add(CreateValue("Taxi Total", "On average, how far does your household travel by taxi each month?"));
                    break;
                
                //Waste
                case QuestionTypes.HouseholdWaste:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/HouseholdWaste.ascx";
                    TextValues.Add(CreateValue("Waste Quantity", "How much waste (recycled and non-recycled) does your household throw out each month?"));
                    TextValues.Add(CreateValue("Waste Quantity Help", "Assume that on average a full black bin bag weights 15kg and a full wheelie bin weighs 30kg."));
                    break;
                case QuestionTypes.GardenWaste:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/GardenWaste.ascx";
                    TextValues.Add(CreateValue("Garden Waste Quantity", "How much garden waste (not composted) does your household throw out each month?"));
                    TextValues.Add(CreateValue("Garden Waste Help", "Assume that on average a full black bin bag weights 15kg and a full wheelie bin weighs 30kg."));
                    break;
                case QuestionTypes.RecycleGlassWaste:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/RecycleGlassWaste.ascx";
                    TextValues.Add(CreateValue("Recycle Glass", "Do you recycle glass in your household?"));
                    break;
                case QuestionTypes.RecyclePaperCardWaste:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/RecyclePaperCardWaste.ascx";
                    TextValues.Add(CreateValue("Recycle Paper", "Do you recycle paper and card in your household?"));
                    break;
                case QuestionTypes.RecycleMetal:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/RecycleMetal.ascx";
                    TextValues.Add(CreateValue("Recycle Metal", "Do you recycle metal cans and foil in your household?"));
                    break;
                case QuestionTypes.RecyclePlasticWaste:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Waste/RecyclePlasticWaste.ascx";
                    TextValues.Add(CreateValue("Recycle Plastic", "Do you recycle plastic containers/ bottles in your household?"));
                    break;
                
                //Water
                case QuestionTypes.Water:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Water/WaterFootprint.ascx";
                    TextValues.Add(CreateValue("Water Bills", "Do you have water usage data available on your recent water bill?"));
                    TextValues.Add(CreateValue("Water Bills Period", "What time period does the bill cover?"));
                    TextValues.Add(CreateValue("Water Bills Amount", "How many units of water did you use during that period?"));
                    TextValues.Add(CreateValue("Washing Machine Usage", "How many times is your household washing machine used per week?"));
                    TextValues.Add(CreateValue("Washing Machine Help", "Please include every wash, regardless of how full the drum is and what temperature you choose."));
                    TextValues.Add(CreateValue("Dish Washer Usage", "How many times is your household  dishwasher used per week?"));
                    TextValues.Add(CreateValue("Dish Washer Help", "Please include every wash, regardless of how full the dishwasher is and what temperature you choose."));
                    TextValues.Add(CreateValue("Bath Usage", "How many baths are taken in your household per week?"));
                    TextValues.Add(CreateValue("Bath Help", "Please enter the number of times you fill the bathtub on average per week."));
                    TextValues.Add(CreateValue("Shower Usage", "How many showers are taken by your household per week?"));
                    break;
                
                //Shopping
                case QuestionTypes.PerishablesShopping:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Shopping/PerishablesShopping.ascx";
                    TextValues.Add(CreateValue("Perishables Shopping", "What is your household's monthly average expenditure on food, beverage (alcoholic and non-alcoholic) and tobacco?"));
                    break;
                case QuestionTypes.ClothingShopping:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Shopping/ClothingShopping.ascx";
                    TextValues.Add(CreateValue("Clothing Shopping", "What is your household's monthly average expenditure on clothing and footwear?"));
                    break;
                case QuestionTypes.HouseholdShopping:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Shopping/HouseholdShopping.ascx";
                    TextValues.Add(CreateValue("Household Shopping", "What is your household's monthly average expenditure on household and personal items?"));
                    break;
                case QuestionTypes.LeisureShopping:
                    UserControlPath = "~/UserControls/CarbonFootprintQuestion/Shopping/LeisureShopping.ascx";
                    TextValues.Add(CreateValue("Leisure Shopping", "What is your household's monthly average expenditure on leisure and recreation?"));
                    break;
                    
                
                    
                    
                default:
                    throw new NotImplementedException("Question Type '{0}' not handled.".Format(QuestionType));
            }
            if(string.IsNullOrEmpty(UserControlPath))
                throw new NotImplementedException("Question Type '{0}' has not set the user control path correctly.".Format(QuestionType));
            LoadDBValues(TextValues, questionId, siteId);
        }

        /// <summary>
        /// Enumertion QuestionType Type to identify the Question in the code
        /// </summary>
        public QuestionTypes QuestionType { get; private set; }

        public int QuestionId { get; private set; }

        /// <summary>
        /// Path for the usercontrol relative the website root
        /// </summary>
        public string UserControlPath { get; private set; }

        private AuditQuestionCustomText CreateValue(string name, string deafultText)
        {
            return new AuditQuestionCustomText() { Name = name, DefaultText = deafultText };
        }

        /// <summary>
        /// Customizable Text Values
        /// </summary>
        public List<AuditQuestionCustomText> TextValues{get; private set;}

        private void LoadDBValues(List<AuditQuestionCustomText> values, int questionId, int siteId)
        {
            CarbonExchange.Bll.QuestionTextFactory factory = new CarbonExchange.Bll.QuestionTextFactory();
            var tmp = factory.GetAllByQuestionId(questionId, siteId);
            foreach (var item in tmp)
            {
                var value = values.Where(x => x.Name.Equals(item.Name, StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault();
                if (value != null)
                {
                    value.DatabaseText = item.Text;
                }
            }
        }

        public class AuditQuestionCustomText
        {
            public string Name { get; set; }
            public string DefaultText { get; set; }
            public string DatabaseText { get; set; }
            public string GetText()
            {
                if (!string.IsNullOrEmpty(DatabaseText))
                {
                    return DatabaseText;
                }
                else
                {
                    return DefaultText;
                }
            }
        }
    }
}
